package com.we823.project.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class MaterialUtil {
	public static String[] getColor(String source) {
		String regex = "\\([灰|黑|红]?\\)";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(source);
		if (matcher.find()) {
			int start = matcher.start();
			int end = matcher.end();
			String color = source.substring(start,end);
			source=source.replace(color, "");
			if (color!=null && color.length()>2) {
				color = color.substring(1,color.length()-1);
			}
			String[] result = {source,color};
			return result;
		}
		return null;
	}
	
	public static Object[] getWire(String source) {
		String regex = "\\(\\d+芯\\)";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(source);
		if (matcher.find()) {
			int start = matcher.start();
			int end = matcher.end();
			String wireString = source.substring(start,end);
			source=source.replace(wireString, "");
			int wire = 0;
			wireString = wireString.substring(1,wireString.length()-2);
			if (StringUtils.isNumeric(wireString)) {
				wire = Integer.parseInt(wireString);
			}
			Object[] result = {source,wire};
			return result;
		}
		return null;
	}
	
	public static String getBomColor(String source) {
		if (StringUtils.isEmpty(source)) {
			return source;
		}
		String regex = "\\d+";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(source);
		if (matcher.find()) {
			int start = matcher.start();
			String color = source.substring(0,start);
			return color.trim();
		}else {
			return source.trim();
		}
	}
}
